<template>
  <div>
    <el-dialog :visible.sync="dialogFormVisible" top="10vh">
      <div class="content">
        <el-form ref="subform1" :model="form" :rules="rules">
          <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" class="form" />
          </el-form-item>
          <el-form-item label="原价" :label-width="formLabelWidth" prop="yprice">
            <div class="detail">
              <el-input-number
                v-model="form.yprice"
                :precision="2"
                :step="0.01"
                placeholder="请输入内容"
                size="medium"
                :min="0"
              />
            </div>
          </el-form-item>
          <el-form-item label="售价" :label-width="formLabelWidth" prop="price">
            <div class="detail">
              <el-input-number
                v-model="form.price"
                :precision="2"
                :step="0.01"
                placeholder="请输入内容"
                size="medium"
                :min="0"
              />
            </div>
          </el-form-item>
          <el-form-item label="邮费" :label-width="formLabelWidth" prop="freight">
            <div class="detail">
              <el-input-number
                v-model="form.freight"
                :precision="0"
                :step="1"
                size="medium"
                :min="0"
              />
            </div>
          </el-form-item>
          <el-form-item label="销量" :label-width="formLabelWidth" prop="sales">
            <div class="detail">
              <el-input-number
                v-model="form.sales"
                :precision="0"
                :step="1"
                size="medium"
                :min="0"
              />
            </div>
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth" prop="sortWidget">
            <div class="detail">
              <el-input-number v-model="form.sortWidget" size="medium" :min="1" />
            </div>
          </el-form-item>
          <!-- 图片 -->
          <el-form-item label="图片尺寸" :label-width="formLabelWidth">
            <el-input v-model="imageTitle" :disabled="true" class="form" />
          </el-form-item>
          <el-form-item label="主图" :label-width="formLabelWidth" prop="coverImage">
            <el-upload
              action="https://mpingweb.yuepinyuemei.com/api/upload"
              :data="{
                dirPath: 'authentication',
                upload_status: true }"
              :multiple="false"
              :limit="1"
              list-type="picture-card"
              :on-remove="handleRemove"
              :on-success="upload_main"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="common.img_url+img_main" alt />
            </el-dialog>
            <img width="20%" :src="common.img_url+form.coverImage" alt />
          </el-form-item>
          <!-- 轮播图/展示图 -->
          <el-form-item label="展示图" :label-width="formLabelWidth" prop="slaveImageList">
            <el-upload
              action="https://mpingweb.yuepinyuemei.com/api/upload"
              :data="{
                dirPath: 'authentication',
                upload_status: true }"
              :multiple="true"
              :limit="5"
              :file-list="img_list"
              :show-file-list="false"
              list-type="picture-card"
              :on-success="upload_arr"
              :before-upload="before_main"
              :on-remove="del_arr"
            >
              <i class="el-icon-plus icon" />
            </el-upload>
            <ul class="img-list">
              <li v-for="(item, index) in img_list" :key="index" class="img-item">
                <img v-if="item.url" :src="item.url" alt class="img" />
                <div class="del-btn" @click.stop="del_arr(item)">
                  <i class="el-icon-delete" />
                </div>
              </li>
            </ul>
          </el-form-item>
          <el-form-item label="内容详情" :label-width="formLabelWidth" prop="des">
            <tinymce id="addGoods" v-model="form.des" :min-height="750" :width="760" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="closeMask">取 消</el-button>
        <el-button v-if="!isDisabled&&infos&&!infos.id" type="primary" plain @click="submitInfo">确 定</el-button>
        <el-button v-if="!isDisabled&&infos&&infos.id" type="primary" plain @click="resetInfo">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addGoods, updateGoods } from "../../api/goods";
import tinymce from "@/components/Tinymce";
export default {
  components: {
    tinymce,
  },
  props: {
    isHidden: {
      type: Boolean,
      default: false,
    },
    isDisabled: {
      type: Boolean,
      default: false,
    },
    detailInfo: {
      id: "",
    },
    //   页面状态  1 添加  2 编辑
    pageStatus: {
      type: Number,
    },
  },
  data() {
    return {
      dialogVisible: false, // 上传图片弹窗是否可见
      formLabelWidth: "120px",
      dialogFormVisible: this.isHidden,
      img_main: "", // 图片
      img_list: [], // 展示图
      img_arr: [], //   项目展示图
      imageTitle: "主图/展示图 ：1:1尺寸，推荐：166*166", // 图片提示信息
      infos: this.detailInfo, // 传过来的信息
      form: {
        name: "", // 名称
        yprice: "", // 展示价
        price: "", // 售价
        freight: 0, // 邮费
        coverImage: "", // 图片
        sales: 0, // 销量
        sortWidget: 0, // 排序
        des: "", // 详情
        slaveImageList: "", // 展示图
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        price: [{ required: true, message: "请输入售价", trigger: "blur" }],
        yprice: [{ required: true, message: "请输入原价", trigger: "blur" }],
        freight: [{ required: true, message: "请输入邮费", trigger: "blur" }],
        sales: [{ required: true, message: "请输入销量", trigger: "blur" }],
        sortWidget: [
          { required: true, message: "请输入排序", trigger: "blur" },
        ],
        coverImage: [
          { required: true, message: "请上传主图", trigger: "blur" },
        ],
        slaveImageList: [
          { required: true, message: "请上传展示图", trigger: "blur" },
        ],
        des: [{ required: true, message: "请输入内容详情", trigger: "blur" }],
      },
    };
  },

  watch: {
    dialogFormVisible() {
      this.$emit("checkMask", this.dialogFormVisible);
    },
    // 展示图
    img_arr(val) {
      const arr = [];
      if (Array.isArray(val)) {
        val.forEach((item, index) => {
          arr.push({
            name: index,
            url: this.common.img_url + item,
          });
        });
        this.img_list = arr;
        this.$forceUpdate();
      }
    },
  },

  mounted() {
    // 说明是查看或者编辑 而不是添加
    if (this.infos && this.infos.id) {
      this.form.name = this.infos.name;
      this.form.yprice = this.infos.yprice;
      this.form.price = this.infos.price;
      this.form.freight = this.infos.freight;
      this.form.sales = this.infos.sales;
      this.form.sortWidget = this.infos.sortWidget;
      this.form.coverImage = this.infos.coverImage;
      this.form.des = this.infos.des;
      this.form.slaveImageList = this.infos.slaveImageList;
      this.img_main = this.infos.coverImg;
      this.img_arr = this.infos.slaveImageList || [];
    }
  },

  methods: {
    // 上传展示图之前钩子
    before_main() {
      this.loading = true;
    },
    // 上传展示图
    upload_arr(res) {
      this.loading = false;
      const arr = [...this.img_arr];
      arr.push(res.data);
      this.img_arr = arr;
    },
    // 删除展示图
    del_arr(val) {
      const del_url = val.url.split(this.common.img_url)[1];
      this.img_arr.forEach((item, index) => {
        if (del_url === item) {
          this.img_arr.splice(index, 1);
        }
      });
    },

    // 取消
    closeMask() {
      this.$refs["subform1"].resetFields();
      this.dialogFormVisible = false;
      this.$emit("checkMask", this.dialogFormVisible);
    },

    // 确认 -添加
    submitInfo() {
      if (this.checkInfo()) {
        addGoods({
          name: this.form.name,
          yprice: this.form.yprice,
          coverImage: this.img_main,
          price: this.form.price,
          freight: this.form.freight,
          sales: this.form.sales,
          sortWidget: this.form.sortWidget,
          slaveImageList: this.img_arr,
          des: this.form.des,
        })
          .then((res) => {
            if (res.data.code === 7001) {
              this.$message.error(res.data.msg);
            } else {
              this.$message({
                showClose: true,
                message: "添加" + res.data.msg,
                type: "success",
              });
              this.$emit("refresh", {});
            }
            this.closeMask();
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          });
      } else {
        // 弹出错误框
        this.$message({
          showClose: true,
          message: "请填写必要参数",
          type: "error",
        });
      }
    },

    // 修改
    resetInfo() {
      if (this.checkInfo()) {
        updateGoods({
          id: this.infos.id,
          name: this.form.name,
          yprice: this.form.yprice,
          coverImage: this.img_main,
          price: this.form.price,
          freight: this.form.freight,
          sales: this.form.sales,
          sortWidget: this.form.sortWidget,
          slaveImageList: this.img_arr,
          des: this.form.des,
        })
          .then((res) => {
            if (res.data.code === 7001) {
              this.$message.error(res.data.msg);
            } else {
              this.$message({
                showClose: true,
                message: "修改" + res.data.msg,
                type: "success",
              });
              this.$emit("refresh", {});
            }
            this.closeMask();
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          });
      } else {
        // 弹出错误框
        this.$message({
          showClose: true,
          message: "请填写必要参数",
          type: "error",
        });
      }
    },

    // 检查参数
    checkInfo() {
      if (!this.img_main) {
        this.img_main = this.form.coverImage;
      }
      return !(
        this.form.name === "" ||
        this.form.name === "" ||
        this.form.yprice === "" ||
        this.form.price === "" ||
        this.form.freight === "" ||
        this.form.sales === ""
      );
    },

    // 图片 -移除
    handleRemove() {
      this.img_main = "";
    },
    // 图片-上传
    upload_main(res) {
      this.img_main = res.data;
    },
  },
};
</script>
<style lang="scss" scoped>
.content {
  padding-right: 1%;
  height: 470px;
  max-height: 480px;
  overflow: auto;
  box-sizing: border-box;
}
.form {
  width: 400px;
  margin-right: 16px;
  margin-bottom: 10px;
}
.detail {
  color: #666;
  min-width: 400px;
  font-size: 16px;
  display: inline-block;
  vertical-align: middle;
}
.img-list {
  display: inline-block;
  vertical-align: middle;
  .img-item {
    width: 150px;
    height: 150px;
    border: 1px solid #ccc;
    display: inline-block;
    vertical-align: middle;
    overflow: hidden;
    margin-right: 10px;
    position: relative;
    .img {
      width: 100%;
    }
    .del-btn {
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.3);
      color: #fff;
      line-height: 150px;
      text-align: center;
      font-size: 40px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      cursor: pointer;
      display: none;
    }
    &:hover {
      .del-btn {
        display: block;
      }
    }
  }
}
</style>
